﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TCCUIControls;
using TCCHWLib.Commands;
using TCCHWLib;

namespace TCCEmulator
{
    public partial class EmulatorForm : Form
    {
        private static EmulatorForm m_EmulatorForm = null;

        public static EmulatorForm TheEmulatorForm
        {
            get
            {
                if (m_EmulatorForm == null)
                    m_EmulatorForm = new EmulatorForm();

                return EmulatorForm.m_EmulatorForm;
            }
            set { EmulatorForm.m_EmulatorForm = value; }
        }

        private EmulatorForm()
        {
            TheEmulatorForm = this;

            uiChip1 = Chip.Instance.UiChip;

            InitializeComponent();

            Size s = uiChip1.Size;
            s.Width += 6;
            s.Height += 36;
            this.Size = s;

            // TEST CODE

          //  Chip.Instance.Processors[0].Cache.Allocate(1);
          //  Chip.Instance.Processors[0].Cache.Allocate(1);
          //  Chip.Instance.Processors[0].Cache.Write(0, 5);
          //  Chip.Instance.Processors[0].Cache.Write(1, 10);

          //  Chip.Instance.Processors[1].Cache.Allocate(1);
          //  Chip.Instance.Processors[1].Cache.Allocate(1);
          //  Chip.Instance.Processors[1].Cache.Write(2, 12);
          //  Chip.Instance.Processors[1].Cache.Write(3, 6);

          //  BatchCommand batch1 = new BatchCommand();

          //  AddCommand add3 = new AddCommand(5, ParameterType.Value, 4, ParameterType.Value);
          //  TxReadCommand read1 = new TxReadCommand(0, ParameterType.Address,0,ParameterType.Value);
          //  TxReadCommand read2 = new TxReadCommand(1, ParameterType.Address,0,ParameterType.Value);
          //  AddCommand add = new AddCommand(1, ParameterType.Instruction,2, ParameterType.Instruction);
          //  AllocateCommand alloc = new AllocateCommand("t1", ParameterType.Value, 1, ParameterType.Value);
          //  TxWriteCommand write = new TxWriteCommand(0, ParameterType.Address, 3, ParameterType.Instruction,0,ParameterType.Value);
          //  TXCommitCommand commit = new TXCommitCommand();

          //  batch1.AddCommand(add3);
          //  batch1.AddCommand(read1);
          //  batch1.AddCommand(read2);
          //  batch1.AddCommand(add);
          ////  batch1.AddCommand(alloc);
          //  batch1.AddCommand(write);
          //  batch1.AddCommand(commit);

          //  BatchCommand batch2 = new BatchCommand();

          //  read1 = new TxReadCommand(0, ParameterType.Address,0,ParameterType.Value);
          //  read2 = new TxReadCommand(3, ParameterType.Address,0,ParameterType.Value);
          //  add = new AddCommand(0, ParameterType.Instruction,1, ParameterType.Instruction);
          //  alloc = new AllocateCommand("t1", ParameterType.Value, 1, ParameterType.Value);
          //  write = new TxWriteCommand(3, ParameterType.Instruction, 2, ParameterType.Instruction,0,ParameterType.Value);



          //  batch2.AddCommand(read1);
          //  batch2.AddCommand(read2);
          //  batch2.AddCommand(add);
          //  batch2.AddCommand(alloc);
          //  batch2.AddCommand(write);


          //  ////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          //  ////++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

          //  TxReadCommand read3 = new TxReadCommand(0, ParameterType.Address, 0, ParameterType.Value);
          //  TxReadCommand read4 = new TxReadCommand(1, ParameterType.Address,0,ParameterType.Value);
          //  AddCommand add2 = new AddCommand(5, ParameterType.Address,6, ParameterType.Address);
          //  TxWriteCommand write5 = new TxWriteCommand(0,ParameterType.Address,7,ParameterType.Instruction,0,ParameterType.Value);
          //  TXCommitCommand commit2 = new TXCommitCommand();

          //  batch2.AddCommand(read3);
          //  batch2.AddCommand(read4);
          //  batch2.AddCommand(add2);
          //  batch2.AddCommand(write5);
          //  batch2.AddCommand(commit2);

          //  //read3 = new ReadCommand(1, ParameterType.Address);
          //  //read4 = new ReadCommand(2, ParameterType.Address);
          //  //add2 = new AddCommand("1", "2", ParameterType.Address, ParameterType.Address);

          //  //batch2.AddCommand(read3);
          //  //batch2.AddCommand(read4);
          //  //batch2.AddCommand(add2);
          //  //batch2.AddCommand(commit2);

          //  batch1.Priority = new PriorityKey(0, 0);
          //  batch2.Priority = new PriorityKey(1, 0);
            
          //  Chip.Instance.Processors[0].EnqueueTask(batch1);
          //  Chip.Instance.Processors[0].EnqueueTask(null);

          //  Chip.Instance.Processors[1].EnqueueTask(batch2);
          //  Chip.Instance.Processors[1].EnqueueTask(null);
        }

        private void EmulatorForm_Activated(object sender, EventArgs e)
        {
            this.Invalidate(true);
        }

        private void EmulatorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Application.Exit();
        }
    }
}
